package com.itheima.service;


import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Member;
import com.itheima.utils.DateUtils;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 快速登录功能
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

  //根据手机号查询是否是会员
    @Override
    public Member findByTelephone(String telephone) {
        Member member = memberDao.findByTelephone(telephone);
        return member;
    }
//新增会员
    @Override
    public void add(Member member) {
        //获取密码
        String password = member.getPassword();
        if (password !=null){
             password = MD5Utils.md5(password);
             member.setPassword(password);
        }
        memberDao.add(member);
    }

    //根据日期查询每个月的会员数量
    @Override
    public List<Integer> findMonthCount(List<String> months) {
        List<Integer> memberCount= new ArrayList<>();
        if (months !=null && months.size()>0){
            for (String month : months) {
                String m = month + "-31";
                Integer memberCountBeforeDate = memberDao.findMemberCountBeforeDate(m);
                memberCount.add(memberCountBeforeDate);
            }
        }
        return memberCount;
    }

    @Override
    public PageResult findMemberByPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Member> pageResult = memberDao.findMemberByPage(queryString);
        long total = pageResult.getTotal();
        List<Member> result = pageResult.getResult();

        return new PageResult(total,result);
    }

    @Override
    public Member findMemberById(Integer id) {
        Member member = memberDao.findById(id);
        return member;
    }

    @Override
    public void editMember(Member member) {
        memberDao.editMember(member);

    }

    @Override
    public void deleteMemberById(Integer id) {
        memberDao.deleteById(id);
    }

    @Override
    public List<Member> findMemberByIds(Integer[] ids) throws Exception {
        List<Member> list = new ArrayList<>();
        for (Integer id : ids) {
            Member member  = memberDao.findById(id);
            list.add(member);
            Date birthday = member.getBirthday();
            String birth = DateUtils.parseDate2String(birthday);
        }
        return list;
    }

    @Override
    public List<Integer> findAgegroupCount() {
        List<Integer> agegroup = new ArrayList<>();

        List<Date> allBir = memberDao.findAllBir();
        List<Calendar> allTime = new ArrayList<>();

        for (Date date : allBir) {
            Calendar calendar2 = Calendar.getInstance();
            calendar2.setTime(date);
            allTime.add(calendar2);
        }
        Calendar calendar = Calendar.getInstance();//**
        Date nowDate = new Date();
        calendar.setTime(nowDate);

        int age1 = 0;
        int age2 = 0;
        int age3 = 0;
        int age4 = 0;
        int age5 = 0;
        int age6 = 0;
        for (Calendar calendar2 : allTime) {
            calendar.add(Calendar.YEAR,-6);
            if (calendar.compareTo(calendar2)>0){
                calendar.add(Calendar.YEAR,-6);
                if (calendar.compareTo(calendar2)>0){
                    calendar.add(Calendar.YEAR,-5);
                    if (calendar.compareTo(calendar2)>0){
                        calendar.add(Calendar.YEAR,-28);
                        if (calendar.compareTo(calendar2)>0){
                            calendar.add(Calendar.YEAR,-24);
                            if (calendar.compareTo(calendar2)>0){
                                age6++;
                            }else {
                                age5++;
                                calendar.add(Calendar.YEAR,69);
                            }
                        }else {
                            age4++;
                            calendar.add(Calendar.YEAR,45);
                        }
                    }else {
                        age3++;
                        calendar.add(Calendar.YEAR,17);
                    }
                }else {
                    age2++;
                    calendar.add(Calendar.YEAR,12);
                }
            }else {
                age1++;
                calendar.add(Calendar.YEAR,6);
            }
        }
        agegroup.add(age1);
        agegroup.add(age2);
        agegroup.add(age3);
        agegroup.add(age4);
        agegroup.add(age5);
        agegroup.add(age6);
        return agegroup;
    }
}
